<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PlayerSkill
 *
 * @author Laszlo.Pokoradi
 */
class PlayerSkill extends Model {
  const TYPE_XPATH = "./@type";
  const CURRENT_XPATH = "./current[1]";
  const POTENTIAL_XPATH = "./potential[1]";
  const FORMINFLUENCED_XPATH = "./form_influenced[1]";
  const TRAININGDAYS_XPATH = "./training_days[1]";

  private $type;
  private $current;
  private $potential;
  private $formInfluenced;
  private $db;

  public function __construct($database) {
    $this->db = $database;

    $this->type = "";
    $this->current = 0;
    $this->potential = 0;
    $this->formInfluenced = 0;
  }

  public function getType() {
    return $this->type;
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerSkill::TYPE_XPATH,$docNode);
    $this->type = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerSkill::CURRENT_XPATH,$docNode);
    $this->current = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerSkill::POTENTIAL_XPATH,$docNode);
    $this->potential = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerSkill::FORMINFLUENCED_XPATH,$docNode);
    $this->formInfluenced = $qResult->item(0)->nodeValue;
  }

  public function toXHTML() {
    $xhtml ="<tr><td>{$this->type}</td><td>{$this->current}</td><td>{$this->potential}</td></tr>";
    return $xhtml;
  }

  public function insertNewToDB($playerId) {
    $this->db->set('playerId',$playerId);
    $this->db->set('type',$this->type);
    $this->db->set('currentSkill',$this->current);
    $this->db->set('potentialSkill',$this->potential);
    $this->db->set('formInfluencedSkill',$this->formInfluenced);
    $query = $this->db->insert('playerskill');

    if (!$query) {
      log_message('error', 'Error during playerskill inserttion!');
      return FALSE;
    }

    return $this->type.':'.$this->db->insert_id();
  }

  public function amendOldInDB($skillId) {
    $this->db->where('id',$skillId);
    $query = $this->db->get('playerskill');

    if (!$query) {
      log_message('error','Error during playerskill amendment!');
    } else {
      $tRow = $query->row();
      $oldId = $tRow->id;
      $playerId = $tRow->playerId;

      if ($tRow->currentSkill!=$this->current
          || $tRow->potentialSkill!=$this->potential
          || $tRow->formInfluencedSkill!=$this->formInfluenced) {
        $newId = $this->insertNewToDB($playerId);

        $this->db->set('playerId', $playerId);
        $this->db->set('table','playerskill');
        $this->db->set('oldId',$oldId);
        $this->db->set('newId',$newId);
        $this->db->set('insertTime',date('Y-m-d H:i:s'));
        $query = $this->db->insert('playerhistory');

        if (!$query) {
          log_message('error','Playerskill amend was unsuccesful!');
        } else {
          return $this->type.':'.$newId;
        }
      }
    }
    return $this->type.':'.$oldId;
  }

  public function loadFromDB($skillId) {
    $this->db->where('id',$skillId);
    $query = $this->db->get('playerskill');

    if (!$query || $query->num_rows()==0) {
      log_message('error', 'PlayerSkill ('.$skillId.') not found in the database!');
    } else {
      $result = $query->row();
      $this->type = $result->type;
      $this->current = $result->currentSkill;
      $this->potential = $result->potentialSkill;
      $this->formInfluenced = $result->formInfluencedSkill;
    }
  }
}
?>
